﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;

namespace outREACH.App_Code
{
    public class FinancialAnnoucement
    {
        public string name { get; set; }
        public int amount { get; set; }
        public string countryDonated { get; set; }
        public string section { get; set; }
        public DateTime dateTimeDonated { get; set; }

        public static List<FinancialAnnoucement> getFinAnnouncement()
        {
            SqlConnection conn = null;
            List<FinancialAnnoucement> result = new List<FinancialAnnoucement>();
            try
            {
                conn = new SqlConnection();
                conn.ConnectionString = ConfigurationManager.ConnectionStrings["outreachdbConnectionString"].ConnectionString;
                conn.Open();
                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandText = "select * from FinancialAnnoucement order by dateTimeDonated desc";
                SqlDataReader dr = comm.ExecuteReader();
                while (dr.Read())
                {
                    FinancialAnnoucement fa = new FinancialAnnoucement();
                    if ((string)dr["name"] == null)
                    {
                        fa.name= "";
                    }
                    else
                    {
                        fa.name= (string)dr["name"];
                    }

                    if ((int)dr["amount"] == 0)
                    {
                        fa.amount = 0;
                    }
                    else
                    {
                        fa.amount = (int)dr["amount"];
                    }

                    if ((string)dr["countryDonated"] == null)
                    {
                        fa.countryDonated = "";
                    }
                    else
                    {
                        fa.countryDonated = (string)dr["countryDonated"];
                    }

                    fa.dateTimeDonated = (DateTime)dr["dateTimeDonated"];

                    if ((string)dr["section"] == null)
                    {
                        fa.section = "";
                    }
                    else
                    {
                        fa.section = (string)dr["section"];
                    }
                    result.Add(fa);
                }
            }
            catch (SqlException e)
            {
                throw e;
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public static void InsertFinAnnoucement(string name, int amount, string countryDonated, string section, DateTime dateTimeDonated)
        {
            SqlConnection conn = null;
            try
            {
                conn = new SqlConnection();
                SqlCommand comm = new SqlCommand();
                conn.ConnectionString = ConfigurationManager.ConnectionStrings["outreachdbConnectionString"].ConnectionString;
                conn.Open();
                comm.Connection = conn;
                comm.CommandText = "insert into FinancialAnnoucement(name, amount, dateTimeDonated, countryDonated, section) VALUES (@name, @amount, @dateTimeDonated, @countryDonated, @section)";
                comm.Parameters.AddWithValue("@name", name);
                comm.Parameters.AddWithValue("@amount", amount);
                comm.Parameters.AddWithValue("@dateTimeDonated", dateTimeDonated);
                comm.Parameters.AddWithValue("@countryDonated", countryDonated);
                comm.Parameters.AddWithValue("@section",section);
                int rowsAffected = comm.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
            }
        }
    }
}